<?php

namespace Beerus\Modules\VirtualPhone\Controllers;

use App\Pockets\GridDataModel as pGridDataModel;
use App\Http\Controllers\Controller;
use Beerus\Modules\VirtualPhone\Actions\DecoShowRealPhone;
use Doraemon\service\BusinessUser\EncryptPhone;
use Encore\Admin\Grid\Displayers\DropdownActions;
use hhz\constant\basic\UserExtendPhoneScene;
use Illuminate\Http\Request;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Doraemon\service\User;
use Doraemon\model\VirtualPhone\VirtualPhoneRecord;
use Doraemon\cache\UserNick;

/**
 * 阿里云虚拟手机号的号池管理
 *
 * @category   Module name
 * @package    PSR
 * @subpackage Documentation\API
 * @author     hujingdong  <hujingdong@haohaozhu.com>
 * @license    GPL http://www.haohaozhu.com
 * @link       http://www.haohaozhu.com
 */

class VirtualPhoneController extends Controller
{
    const PAGE_SIZE = 20;

    public function __construct()
    {
        parent::__construct();
        Admin::script('$(".pjax-container").attr("id","pjax-container");');
    }

    public function index(Request $request, Content $content)
    {
        $request_data = $request->all();

        $page_size = $request->input("per_page", self::PAGE_SIZE);
        $page = $request->input("page", 1);

        $condition = [];
        if (!empty($request_data['uid'])) {
            $condition['user_uid'] = $request_data['uid'];
        }
        if (!empty($request_data['deco_uid'])) {
            $condition['deco_uid'] = $request_data['deco_uid'];
        }
        if (!empty($request_data['user_phone'])) {
            $condition['hidden_user_phone'] = EncryptPhone::getHiddenPhone($request_data['user_phone']);
        }
        if (!empty($request_data['deco_phone'])) {
            $condition['hidden_deco_phone'] = EncryptPhone::getHiddenPhone($request_data['deco_phone']);
        }
        if (!empty($request_data['virtual_phone'])) {
            $condition['virtual_phone'] = $request_data['virtual_phone'];
        }


        $condition['ORDER'] = ['id' => 'DESC'];
        $condition['LIMIT'] = [
            ($page - 1) * $page_size,
            $page_size
        ];

        $lists = VirtualPhoneRecord::select('*', $condition);

        $total = 0;
        if (!empty($lists)) {
            unset($condition['ORDER']);
            unset($condition['LIMIT']);
            $total = VirtualPhoneRecord::count($condition);
        }

        $gridModel = new pGridDataModel($lists, $total, [
            'perPage' => $page_size
        ]);
        $gridList = Admin::grid(
            $gridModel,
            function (Grid $grid) use ($gridModel) {
                $grid->paginate($gridModel->getPerPage());
                $grid->disableExport();
                $grid->disableRowSelector();
                $grid->setActionClass(DropdownActions::class);

                $grid->filter(function (Grid\Filter $filter) {
                    $filter->disableIdFilter();
                    $filter->column(1 / 2, function ($filter) {
                        $filter->equal('deco_uid', '装企uid')->placeholder('请输入装修公司uid');
                        $filter->equal('uid', '用户uid')->placeholder('请输入用户的uid');
                    });

                    $filter->column(1 / 2, function ($filter) {
//                        $filter->equal('user_phone', '用户手机号')->placeholder('请输入用户手机号');
//                        $filter->equal('deco_phone', '装企手机号')->placeholder('请输入装企手机号');
                        $filter->equal('virtual_phone', '虚拟手机号')->placeholder('请输入虚拟手机号');
                    });


                });

                $grid->column("id", 'Id');
                $grid->column("user_uid", '用户uid');
                $grid->column("user_nick", '用户昵称')->display(function () {
                    return UserNick::uid2nick($this->user_uid);
                });
                $grid->column("hidden_user_phone", '用户真实手机号');
                $grid->column('virtual_phone', '虚拟手机号');
                $grid->column('virtual_phone_area', '虚拟手机号号码归属地');
                $grid->column("deco_uid", '装修公司uid');
                $grid->column("deco_nick", '装修公司昵称')->display(function () {
                    return UserNick::uid2nick($this->deco_uid);
                });
                $grid->column("decrypt_deco_phone", '装修公司回拨号码')->display(function () {
                    return $this->hidden_deco_phone;
                });
                $grid->column("deco_area_name", '装修公司主要服务地区');
                $grid->column("addtime", '生效时间')->display(function () {
                    return date("Y-m-d H:i:s",$this->addtime);
                });
                $grid->column("virtual_expire_time", '失效时间');
                $grid->actions(function (Grid\Displayers\Actions $actions) {
                    $actions->disableDelete();
                    //增加查看用户真实手机号码的逻辑,方便查看
                    if (in_array(Admin::user()->username,["hujingdong","zhangzhijie"])) {
                        //$actions->add(new ReleaseVirtualPhone);
                        $actions->add(new DecoShowRealPhone);
                    }
                });
            }
        );

        return $content
            ->header('虚拟手机号列表')
            ->row($gridList);
    }

}
